package com.gs.data.convert;

import com.gs.common.tool.pojo.OptionItem;
import com.gs.common.tool.utils.SwingUtil;
import com.gs.data.convert.enums.BizEnum;
import com.gs.data.convert.enums.DelimiterEnum;
import com.gs.data.convert.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.util.Arrays;
import java.util.Objects;


/**
 * 启动类
 * @author guos
 * @date 2025/10/15 11:07
 **/
@Slf4j
public class App {

    private static final String fontName = "新宋体";


    public static void main(String[] args) {
        //使用事件调度线程确保所有的Swing组件都在AWT线程中安全地构建
        SwingUtilities.invokeLater(App::createJTextPaneAndShowGUI);
    }


    /**
     * 创建gui
     */
    private static void createJTextPaneAndShowGUI() {
        // 创建主窗口
        JFrame frame = new JFrame("数据处理工具");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 创建GridBagLayout实例
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 5, 5, 5); // 内边距
        gbc.gridx = 0; // 列位置从0开始
        gbc.gridy = 0; // 行位置从0开始
        gbc.weightx = 0; // 水平权重
        gbc.weighty = 0; // 垂直权重

        // 添加GridBagLayout到窗口
        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new GridBagLayout());

        //第1行: JLabel和JComboBox
        JLabel label1 = new JLabel("功能列表:");
        label1.setFont(new Font(fontName, Font.BOLD, 14));
        gbc.gridy = 0;
        contentPane.add(label1, gbc);
        gbc.gridx = 1; // 移动到下一列
        JComboBox<OptionItem> comboBox1 = new JComboBox<>(getBizList());
        contentPane.add(comboBox1, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        JLabel label2 = new JLabel("分隔符:");
        label2.setFont(new Font(fontName, Font.BOLD, 14));
        contentPane.add(label2, gbc);
        gbc.gridx = 1; // 移动到下一列
        JComboBox<OptionItem> comboBox2 = new JComboBox<>(getDelimiterList());
        contentPane.add(comboBox2, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        //第3行: 标签、JButton
        JLabel label3 = new JLabel("请将原数据，复制到↓↓↓");
        label3.setFont(new Font(fontName, Font.BOLD, 14));
        label3.setForeground(Color.RED);
        contentPane.add(label3, gbc);
        gbc.gridx = 1;
        JButton clickButton = new JButton("开始生成");
        contentPane.add(clickButton, gbc);

        gbc.gridy++;
        //第四行: JTextArea第1列
        gbc.gridx = 0;
        gbc.gridwidth = 1; // 列
        gbc.weightx = 1; // 允许水平扩展
        gbc.weighty = 1; // 允许垂直扩展

        JTextPane inputTextPane = new JTextPane();
        inputTextPane.setEditable(true);
        contentPane.add(new JScrollPane(inputTextPane), gbc);

        //第四行: JTextArea第2列
        gbc.gridx = 1;
        gbc.gridwidth = 1; //列
        gbc.weightx = 1; // 允许水平扩展
        gbc.weighty = 1; // 允许垂直扩展

        JTextPane outputTextPane = new JTextPane();
        outputTextPane.setEditable(false);
        contentPane.add(new JScrollPane(outputTextPane), gbc);

        //获取StyledDocument用于格式化文本
        StyledDocument doc = outputTextPane.getStyledDocument();
        //创建样式并设置颜色: 红色、蓝色、绿色
        SimpleAttributeSet redAttr = SwingUtil.getAttributeSet(Color.RED);
        SimpleAttributeSet blueAttr = SwingUtil.getAttributeSet(Color.BLUE);

        //按钮点击事件
        clickButton.addActionListener(e -> {
            //先清空输出区域
            outputTextPane.setText("");
            OptionItem biz = (OptionItem) comboBox1.getSelectedItem();
            if (Objects.isNull(biz)) {
                SwingUtil.writeRowText(doc, redAttr, "请选择相关功能!");
                return;
            }
            OptionItem delimiterInfo = (OptionItem) comboBox2.getSelectedItem();
            if (Objects.isNull(delimiterInfo)) {
                SwingUtil.writeRowText(doc, redAttr, "请选择分割符!");
                return;
            }
            //获取inputTextPane
            String text = inputTextPane.getText();
            if (StringUtil.isEmpty(text)) {
                SwingUtil.writeRowText(doc, redAttr, "原数据不能为空!");
                return;
            }
            String delimiter = delimiterInfo.getKey();
            //执行结果
            String res = null;
            try {
                switch (BizEnum.getByKey(biz.getKey())) {
                    case JOIN_SINGLE_QUOT:
                        res = StringUtil.joinSingleQuot(text, delimiter);
                        break;
                    case JOIN_DOUBLE_QUOT:
                        res = StringUtil.joinDoubleQuot(text, delimiter);
                        break;
                    case DOUBLE_QUOT_TO_SINGLE:
                        res = StringUtil.doubleQuotToSingle(text, delimiter);
                        break;
                    case SINGLE_QUOT_TO_DOUBLE:
                        res = String.join(",", StringUtil.singleQuotToDouble(text, delimiter));
                    default:
                        break;
                }
                SwingUtil.writeRowText(doc, blueAttr, res);
            } catch (Exception ex) {
                SwingUtil.writeRowText(doc, redAttr, ex.getMessage());
            }
        });

        //自定义下拉框显示
        SwingUtil.showComboBox(comboBox1);
        SwingUtil.showComboBox(comboBox2);

        // 设置窗口大小并使其可见
        frame.pack(); // 自动调整窗口大小以适应其组件
        frame.setSize(800, 600); // 设置窗口初始大小
        frame.setVisible(true);
    }


    /**
     * 获取业务下拉列表
     */
    private static OptionItem[] getBizList() {
        return Arrays.stream(BizEnum.values()).map(d -> {
            OptionItem optionItem = new OptionItem();
            optionItem.setKey(d.getKey()).setValue(d.getName());
            return optionItem;
        }).toArray(OptionItem[]::new);
    }


    /**
     * 获取分割符下拉列表
     */
    private static OptionItem[] getDelimiterList() {
        return Arrays.stream(DelimiterEnum.values()).map(d -> {
            OptionItem optionItem = new OptionItem();
            optionItem.setKey(d.getKey()).setValue(d.getName());
            return optionItem;
        }).toArray(OptionItem[]::new);
    }
}
